class PlayCard {
	// 本轮的胜利者
	public static roundWinner: Player = null;
	// 本轮最大的牌
	public static winCard = null;
	// 玩家当前选中的牌
	public static readyCards = [];
	/**
	 * 轮换出牌
	*/
	public static async playing(player: Player) {
		var self = this;
		var result = null;

		// 清场
		// 1.提示文字清空
		player.robLanText.text = '';
		// 2. 出牌区的牌要清空
		for (var i: number = 0; i < player.myPrevPlayCardsObj.length; i++) {
			var bitMap = player.myPrevPlayCardsObj[i];
			if (bitMap.parent) {
				bitMap.parent.removeChild(bitMap);
			}
			// 删除手牌的bitmap对象
			for (var j: number = 0; j < player.myCardsObj.length; j++) {
				if (player.myPrevPlayCardsObj[i] == player.myCardsObj[j]) {
					player.myCardsObj.splice(j, 1);
				}
			}
		}

		// 重新分析牌型
		AILogic.analyse(player);

		if (player.isAI) {
			// 延迟1秒
			await Data.sleep(1000);
			// 
			if (!PlayCard.roundWinner || PlayCard.roundWinner == player) {
				// 上一轮赢牌,出牌
				result = AILogic.play(player, Data.currentLandlord.cardList.length);
			} else {
				// 跟牌
				result = AILogic.follow(player, PlayCard.winCard, PlayCard.roundWinner.isLandlord, PlayCard.roundWinner.cardList.length);
			}
			console.log(result);

			if (result) {
				// 成功出牌
				for (var i: number = 0; i < result.cardList.length; i++) {
					// 删除手牌
					for (var j: number = 0; j < player.cardList.length; j++) {
						// 删除手牌的数据
						if (player.cardList[j].val === result.cardList[i].val && player.cardList[j].type === result.cardList[i].type) {
							player.cardList.splice(j, 1);
							break;
						}
					}
				}
				// 如果有炸弹,则更新倍数
				if (result.cardKind === GameRule.isBomb || result.cardKind === GameRule.BOMB) {

				}
				PlayCard.roundWinner = player;
				// delete result.cardList;
				PlayCard.winCard = result;
				// 重绘 页面信息(电脑卡牌数量)
				PlayCard.reDraw(player, result.cardList);
			} else {
				// 不出牌
				player.robLanText.text = '过!';
			}
			if (player.cardList.length == 0) {
				PlayCard.judgeWiner(player);
				return;
			}
			// 继续出牌
			PlayCard.playing(player.nextPlayer);
		} else {
			// 轮到我出牌了!
			console.log('轮到我出牌了!');
			// 玩家的提示次数清空
			SceneManager.getInstance().gamePlayingPanel.promptIndex = 0;
			// 显示出牌的按钮组
			for (var i: number = 0; i < SceneManager.getInstance().gamePlayingPanel.playBtns.btnArr.length; i++) {
				var btn = SceneManager.getInstance().gamePlayingPanel.playBtns.btnArr[i];
				SceneManager.getInstance().gamePlayingPanel.playBtns.addChild(btn);
				if (i == 2) {
					btn._bg.texture = RES.getRes('warn')
					btn.touchEnabled = false;
				}

			}

			SceneManager.getInstance().gamePlayingPanel.playBtns
			// PlayCard.playing(player.nextPlayer);
			// 如果本轮自己赢牌,则出牌的时候不显示  [不出]  按钮
			if (!PlayCard.roundWinner || PlayCard.roundWinner.name == player.name) {
				var btn = SceneManager.getInstance().gamePlayingPanel.playBtns.btnArr[0];
				SceneManager.getInstance().gamePlayingPanel.playBtns.removeChild(btn);
			}
		}
	}


	/**
	 * 判断输赢
	*/
	public static judgeWiner(player: Player) {
		// alert(player.name + '赢了')

		for (var i: number = 0; i < Data.playerArr.length; i++) {
			var p = Data.playerArr[i];
			console.log(p.name)
			// 清场
			// 1.提示文字清空
			// p.robLanText.text = '';
			// // 2. 出牌区的牌要清空
			// for (var j: number = 0; j < p.myPrevPlayCardsObj.length; j++) {
			// 	var bitMap = p.myPrevPlayCardsObj[j];

			// 	console.log(p.myPrevPlayCardsObj);

			// 	if (bitMap.parent) {
			// 		bitMap.parent.removeChild(bitMap);
			// 	}
			// }

			// 展示未出牌区域牌面
			for (var j: number = 0; j < p.myCardsObj.length; j++) {
				var bitMap = p.myCardsObj[j];
				bitMap.texture = RES.getRes(p.cardList[j].icon.split('.')[0]);
			}
			// }
		}




		// setTimeout(function () {
		// 	// 重新开始
		// 	SceneManager.getInstance().gamePlayingPanel.start();
		// }, 2000);
		// 显示重新开始按钮
		SceneManager.getInstance().gamePlayingPanel.addChild(SceneManager.getInstance().gamePlayingPanel.resetBtn);


	}

	/**
	 * 判断玩家选中的牌是否是正确的牌型,出牌需要符合规则,跟牌需要牌型大过上家
	*/
	public static getReadyCardsKind() {
		PlayCard.readyCards = [];
		var player: Player = Data.playerArr[2];
		for (var i: number = 0; i < player.myCardsObj.length; i++) {
			if (player.myCardsObj[i].isSelected) {
				PlayCard.readyCards.push(player.myCardsObj[i].info);
			}
		}

		if (PlayCard.readyCards.length === 0) {
			return null;
		}
		var type = GameRule.typeJudge(PlayCard.readyCards);


		if (type) {
			// 牌型正确
			// 王炸大过任何
			// 炸弹大过其他
			// 同牌型比大小
			if (!PlayCard.winCard || PlayCard.roundWinner.name == player.name) {
				return type;
			}

			if (type.cardKind === GameRule.KING_BOMB || (type.cardKind === GameRule.BOMB && PlayCard.winCard.cardKind != GameRule.BOMB) || (type.cardKind === PlayCard.winCard.cardKind && PlayCard.winCard.size === type.size && type.val > PlayCard.winCard.val)) {
				return type;
			}

			return null;
		} else {
			return null;
		}

	}
	/**
	 * 重绘出牌区域
	*/
	public static reDraw(player: Player, arr) {
		// 拿到出牌对应的数量bitmap

		player.myPrevPlayCardsObj = player.myCardsObj.splice(player.myCardsObj.length - arr.length);
		for (var i: number = 0; i < arr.length; i++) {
			var bitMap = player.myPrevPlayCardsObj[i];
			if (bitMap) {
				bitMap.x = (Data.getStageW() - 646) / 2 + 30 * i;
				bitMap.y = (Data.getStageH() - 223) / 2 - 71;
				bitMap.texture = RES.getRes(arr[i].icon.split('.')[0]);
				if (player.name == 'right-player') {
					// 右侧玩家
					bitMap.x = -105 - (arr.length - i - 1) * 30;
				}
			}
		}
	}

	/**
	 * 加提示出牌
	*/
	public static prompt(winc, player: Player) {
		var stat = GameRule.valCount(player.cardList);

		if (!PlayCard.roundWinner || PlayCard.roundWinner.name == player.name) {
			// 出牌
			console.log('出牌提示');
			
			var cards = player.cardList.slice(0);
			var promptList = [];
			for (var i = stat.length - 1; i >= 0; i--) {
				
				if (i != 0) {
					promptList.push(cards.splice(cards.length - stat[i].count, cards.length - 1));
				} else {
					promptList.push(cards);
				}
			}
			console.log(promptList);
			return promptList;


		} else {
			console.log('跟牌提示');
			console.log(winc.cardKind);
			// 跟牌
			// 声明提示牌的数组
			var promptList = [];
			switch (winc.cardKind) {
				case GameRule.ONE:
					promptList = PlayCard.setPrompt(1, winc.val, stat, player);
					// 单牌
					break;
				case GameRule.PAIRS:
					promptList = PlayCard.setPrompt(2, winc.val, stat, player);
					// 对子
					break;
				case GameRule.THREE:
					promptList = PlayCard.setPrompt(3, winc.val, stat, player);
					// 三根
					break;
				case GameRule.THREE_WITH_ONE:
					var threePrompt = PlayCard.setPrompt(3, winc.val, stat.slice(0), player),
						onePrompt = PlayCard.setPrompt(1, 2, stat.slice(0), player);
						console.log(threePrompt);
						console.log(onePrompt);						
					for (var i: number = 0; i < threePrompt.length; i++) {
						for (var j: number = 0; j < onePrompt.length; j++) {
							if (onePrompt[j][0].val != threePrompt[i][0].val) {
								promptList.push(threePrompt[i].concat(onePrompt[j]));
							}
						}
					}
					// 三带一
					break;
				case GameRule.THREE_WITH_PAIRS:
					// 三带对
					var threePrompt = PlayCard.setPrompt(3, winc.val, stat.slice(0), player),
						pairsPrompt = PlayCard.setPrompt(2, 2, stat.slice(0), player);
					for (var i = 0; i < threePrompt.length; i++) {
						for (var j = 0; j < pairsPrompt.length; j++) {
							if (pairsPrompt[j][0].val != threePrompt[i][0].val) {
								promptList.push(threePrompt[i].concat(pairsPrompt[j]));
							}
						}
					}
					break;
				case GameRule.PROGRESSION:
					// 顺子				
					if (winc.val < 14 && player.cardList.length >= winc.size) {//不是最大顺子才有的比
						for (var i: number = winc.val + 1; i < 15; i++) {
							var proList = [];
							for (var j = 0; j < player.cardList.length; j++) {
								if (player.cardList[j].val < i && proList.length === 0) break;
								if (player.cardList[j].val > i || (proList.length > 0 && player.cardList[j].val === proList[proList.length - 1].val)) {
									continue;
								}
								if (player.cardList[j].val === i && proList.length === 0) {
									proList.push(player.cardList.slice(j, j + 1)[0]);
									continue;
								}
								if (proList[proList.length - 1].val - 1 === player.cardList[j].val) {//判定递减
									proList.push(player.cardList.slice(j, j + 1)[0]);
									if (proList.length === winc.size) {
										promptList.push(proList);
										break;
									}
								} else { break; }
							}
						}
					}
					break;
				case GameRule.PROGRESSION_PAIRS:
					// 连对
					if (winc.val < 14 && player.cardList.length >= winc.size) {//不是最大顺子才有的比
						for (var i: number = winc.val + 1; i < 15; i++) {
							var proList = [];
							for (var j = 0; j < player.cardList.length; j++) {
								if (player.cardList[j].val < i && proList.length === 0) break;
								if (player.cardList[j].val > i || (proList.length > 0 && player.cardList[j].val === proList[proList.length - 1].val)) {
									continue;
								}
								if (player.cardList[j].val === i && player.cardList[j + 1] && player.cardList[j + 1].val === i && proList.length === 0) {
									proList = proList.concat(player.cardList.slice(j, j + 2));
									j++;
									continue;
								}
								if (proList.length > 0
									&& proList[proList.length - 1].val - 1 === player.cardList[j].val
									&& player.cardList[j + 1]
									&& proList[proList.length - 1].val - 1 === player.cardList[j + 1].val) {//判定递减
									proList = proList.concat(player.cardList.slice(j, j + 2));
									j++;
									if (proList.length === winc.size) {
										promptList.push(proList);
										break;
									}
								} else { break; }
							}
						}
					}
					break;
				case GameRule.PLANE:
					// 飞机
					promptList = PlayCard.getPlanePrompt(winc, winc.size / 3, player);
					break;
				case GameRule.PLANE_WITH_ONE:
					promptList = PlayCard.getPlanePrompt(winc, winc.size / 4, player);
					// 飞机带单
					break;
				case GameRule.PLANE_WITH_PAIRS:
					// 飞机带对
					promptList = PlayCard.getPlanePrompt(winc, winc.size / 5, player);
					break;
				case GameRule.FOUR_WITH_TWO:
					// 四带二单
					promptList = PlayCard.setPrompt(4, winc.val, stat, player);

					break;
				case GameRule.FOUR_WITH_TWO_PAIRS:
					// 四带二对
					promptList = PlayCard.setPrompt(4, winc.val, stat, player);

					break;
				case GameRule.BOMB:
					promptList = PlayCard.setPrompt(4, winc.val, stat, player);
				// 炸弹
				default:
					break;
			}
			if (winc.cardKind != GameRule.KING_BOMB && winc.cardKind != GameRule.BOMB) {
				//炸弹加入
				if (player.bomb.length > 0) {
					for (var i = player.bomb.length - 1; i >= 0; i--) {
						promptList.push(player.bomb[i].cardList);
					}
				}
			}
			if (winc.cardKind != GameRule.KING_BOMB) {
				//王炸加入
				if (player.kingBomb.length > 0) {
					promptList.push(player.kingBomb[0].cardList);
				}
			}
			return promptList;
		}
	}
	/**
	 * 设置符合条件的提示牌
	 * c,几张的牌
	 * winVal,要求大过的牌
	 * st 牌统计
	 * 当前用户
	 * PlayCard.setPrompt(2, winc.val, stat, player);
	*/
	public static setPrompt(c, winVal, st, player: Player) {
		var result = [];
		//除去不能大过当前的牌
		for (var i = st.length - 1; i >= 0; i--) {
			if (st[i].count < c || st[i].val <= winVal) {
				st.splice(i, 1);
			}
		}
		st.sort(function (a, b) {
			if (a.count === b.count) {
				return a.val > b.val ? 1 : -1;
			} if (a.count < b.count) {
				return -1;
			} else {
				return 1;
			}
		});
		//加入各个符合值的单牌
		console.log(st);
		console.log(player.cardList);
		// 给player.cardList排序
		player.cardList.sort(Data.bTos);
		for (i = 0; i < st.length; i++) {
			for (var j = 0; j < player.cardList.length; j++) {
				if (player.cardList[j].val === st[i].val) {
					result.push(player.cardList.slice(j, j + c));
					break;
				}
			}
		}
		return result;
	}
	/**
	 * 获取三顺的提示牌
	 * n 数量 有几个三根
	*/
	public static getPlanePrompt(winc, n, player: Player) {

		var result = [];
		if (winc.val < 14 && player.cardList.length >= winc.size) {//不是最大顺子才有的比
			for (var i: number = winc.val + 1; i < 15; i++) {
				var proList = [];
				for (var j = 0; j < player.cardList.length; j++) {
					if (player.cardList[j].val < i && proList.length === 0) break;
					if (player.cardList[j].val > i || (proList.length > 0 && player.cardList[j].val === proList[proList.length - 1].val)) {
						continue;
					}
					if (player.cardList[j].val === i
						&& player.cardList[j + 1]
						&& player.cardList[j + 1].val === i
						&& player.cardList[j + 2]
						&& player.cardList[j + 2].val === i
						&& proList.length === 0) {
						proList = proList.concat(player.cardList.slice(j, j + 3));
						j += 2;
						continue;
					}
					if (proList.length > 0
						&& proList[proList.length - 1].val - 1 === player.cardList[j].val
						&& player.cardList[j + 1]
						&& proList[proList.length - 1].val - 1 === player.cardList[j + 1].val
						&& player.cardList[j + 2]
						&& proList[proList.length - 1].val - 1 === player.cardList[j + 2].val) {//判定递减
						proList = proList.concat(player.cardList.slice(j, j + 3));
						j += 2;
						if (proList.length === n * 3) {
							result.push(proList);
							break;
						}
					} else {
						break;
					}
				}
			}
		}
		return result;
	}


}